package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.MallOrderPayBo;
import com.rla.domain.vo.MallOrderPayVo;
import com.rla.domain.MallOrderPay;
import com.rla.mapper.MallOrderPayMapper;
import com.rla.service.IMallOrderPayService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单支付Service业务层处理
 *
 * @author rla
 * @date 2025-08-11
 */
@RequiredArgsConstructor
@Service
public class MallOrderPayServiceImpl implements IMallOrderPayService {

    private final MallOrderPayMapper baseMapper;

    /**
     * 查询订单支付
     */
    @Override
    public MallOrderPayVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单支付列表
     */
    @Override
    public TableDataInfo<MallOrderPayVo> queryPageList(MallOrderPayBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrderPay> lqw = buildQueryWrapper(bo);
        Page<MallOrderPayVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单支付列表
     */
    @Override
    public List<MallOrderPayVo> queryList(MallOrderPayBo bo) {
        LambdaQueryWrapper<MallOrderPay> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOrderPay> buildQueryWrapper(MallOrderPayBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrderPay> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), MallOrderPay::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getPayMode()), MallOrderPay::getPayMode, bo.getPayMode());
        lqw.eq(bo.getPaymentAmount() != null, MallOrderPay::getPaymentAmount, bo.getPaymentAmount());
        lqw.eq(bo.getPaymentTime() != null, MallOrderPay::getPaymentTime, bo.getPaymentTime());
        lqw.eq(bo.getBackAmount() != null, MallOrderPay::getBackAmount, bo.getBackAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSettlement()), MallOrderPay::getIsSettlement, bo.getIsSettlement());
        lqw.eq(StringUtils.isNotBlank(bo.getSettlementId()), MallOrderPay::getSettlementId, bo.getSettlementId());
        lqw.eq(StringUtils.isNotBlank(bo.getExpandJson()), MallOrderPay::getExpandJson, bo.getExpandJson());
        return lqw;
    }

    /**
     * 新增订单支付
     */
    @Override
    public Boolean insertByBo(MallOrderPayBo bo) {
        MallOrderPay add = BeanUtil.toBean(bo, MallOrderPay.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单支付
     */
    @Override
    public Boolean updateByBo(MallOrderPayBo bo) {
        MallOrderPay update = BeanUtil.toBean(bo, MallOrderPay.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrderPay entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单支付
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
